﻿Imports System.Data.OracleClient
Imports WindowsApplication1.funciones1
'Imports System.Data
Imports Microsoft.Data.Odbc
Public Class FACTURA
    Dim conexion As String = "Driver={Microsoft ODBC for Oracle};Server=xe;Uid=redes;Pwd=redes;"
    Dim conexion2 As String = "Server=xe;Uid=redes;Pwd=redes;"

    Dim querysecuencia As String = "select max(ID_FACTURA)+ 1 as id_factura from TB_VNT_FACTURA"
    Dim camposecuencia As String = "ID_FACTURA"
    Dim temp As String = traersecuencia(querysecuencia, camposecuencia)
    Dim VectorDeDatos(1) As TextBox
    Public Function LlenarVector() As TextBox()
        VectorDeDatos(0) = txt_fac2
        VectorDeDatos(1) = txt_orden2
        Return VectorDeDatos
    End Function
    Private Sub FACTURA_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim combo1() As String = {"A", "B", "C"}
        cmb_serie.Items.AddRange(combo1)
        Dim combo() As String = {"Si", "No"}
        cmb_comision.Items.AddRange(combo)
        Dim combo2() As String = {"Personal", "Logistica"}
        cmb_envio.Items.AddRange(combo2)
        Dim combo3() As String = {"Efectivo", "Credito"}
        cmb_forma.Items.AddRange(combo3)

        VectorDeDatos = LlenarVector()

        BarraNavegacion1.Inicializar("Driver={Microsoft ODBC for Oracle};Server=xe;Uid=redes;Pwd=redes;", "TB_VNT_DETTAB_FACTURA", VectorDeDatos)
        txt_idfac.Text = temp
        Dim querycombo2 As String = "SELECT * FROM TB_AFA_SOCIO"
        Dim valuecombo2 As String = "ID_SOCIO" 'valor a insertar en tabla de acuerdo al combo
        Dim displaycombo2 As String = "NOMBRE_SOCIO" 'valor a mostrar en el combo
        llenarcombo(cmb_socio, querycombo2, valuecombo2, displaycombo2)
        Dim querycombo As String = "SELECT * FROM TB_VNT_ORDEN_VENTA"
        Dim valuecombo As String = "ID_ORVEN" 'valor a insertar en tabla de acuerdo al combo
        Dim displaycombo As String = "ID_ORVEN" 'valor a mostrar en el combo
        llenarcombo(cmb_orden, querycombo, valuecombo, displaycombo)

        txt_comision.Text = "No"


    End Sub

    Private Sub cmb_orden_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_orden.TextChanged
        If cmb_orden.ValueMember.Any Then
            txt_orden.Text = cmb_orden.SelectedValue
        End If
        
    End Sub

    Private Sub txt_orden_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_orden.TextChanged
        Dim querycombo4 As String = "SELECT * FROM TB_VNT_ORDEN_VENTA WHERE ID_ORVEN=" & Me.txt_orden.Text & ""
        Dim valuecombo4 As String = "ID_ORVEN" 'valor a insertar en tabla de acuerdo al combo
        Dim displaycombo4 As String = "ESTADO" 'valor a mostrar en el combo
        llenarcombo(cmb_estado, querycombo4, valuecombo4, displaycombo4)
        txt_fac2.Text = txt_idfac.Text
        txt_orden2.Text = txt_orden.Text


    End Sub

    Private Sub cmb_serie_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_serie.TextChanged

        txt_serie.Text = cmb_serie.SelectedItem


    End Sub

    Private Sub cmb_envio_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_envio.TextChanged

        txt_envio.Text = cmb_envio.SelectedItem


    End Sub

    Private Sub cmb_socio_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_socio.TextChanged
        If cmb_socio.ValueMember.Any Then
            txt_socio.Text = cmb_socio.SelectedValue
        End If

    End Sub

    Private Sub cmb_estado_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_estado.TextChanged

        txt_estado.Text = cmb_estado.Text

    End Sub

    Private Sub txt_idfac_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_idfac.EnabledChanged
        
        If txt_idfac.Enabled = True Then
            txt_idfac.Enabled = False
            cmb_estado.Enabled = False


        End If
        If txt_fecha.Text = "" Then
            txt_fecha.Text = Date.Today.ToShortDateString

        End If
       
    End Sub

    Private Sub btn_guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_guardar.Click
        Dim oConexion As OdbcConnection
        oConexion = New OdbcConnection(conexion)
        Dim queryString As String = "INSERT INTO TB_VNT_FACTURA VALUES " & _
        " ('" & Me.txt_idfac.Text & "','" & Me.txt_socio.Text & "','" & Me.txt_fecha.Text & "','" & Me.txt_serie.Text & "','" & Me.txt_envio.Text & "','" & Me.txt_forma.Text & "','" & Me.txt_estado.Text & "','" & Me.txt_comision.Text & "','" & Me.txt_total.Text & "')"
        Dim command As New OdbcCommand(queryString)
        command.Connection = oConexion
        oConexion.Open()
        command.ExecuteNonQuery()

        oConexion.Close()
        MessageBox.Show("Validacion exitosa.", "Operacion Exitosa", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub cmb_forma_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_forma.TextChanged

        txt_forma.Text = cmb_forma.SelectedItem

    End Sub


    Private Sub btn_actualiza_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_actualiza.Click
        Dim querydgv As String = "select b.id_orven,a.nombre,b.cantidad,b.precio,b.subtotal from tb_ina_producto a, tb_vnt_dettab_ov b   where a.id_producto = b.id_producto And id_orven = " & Me.txt_orden.Text & ""
        dgv_fac.DataSource = LlenarDgv(querydgv)


        Dim querycombo4 As String = "SELECT SUM(SUBTOTAL) AS SUBTOTAL FROM TB_VNT_DETTAB_OV WHERE ID_ORVEN=" & Me.txt_orden.Text & ""
        Dim valuecombo4 As String = "SUBTOTAL" 'valor a insertar en tabla de acuerdo al combo
        Dim displaycombo4 As String = "SUBTOTAL" 'valor a mostrar en el combo
        llenarcombo(cmb_total, querycombo4, valuecombo4, displaycombo4)


    End Sub

    Private Sub cmb_total_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_total.TextChanged
        txt_total.Text = cmb_total.Text

    End Sub

    Private Sub txt_idfac_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_idfac.TextChanged
      
    End Sub

    Private Sub txt_orden2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_orden2.TextChanged
        Dim querysecuencia2 As String = "select max(ID_FACTURA)+ 1 as id_factura from TB_VNT_FACTURA"
        Dim camposecuencia2 As String = "ID_FACTURA"
        Dim temp2 As String = traersecuencia(querysecuencia, camposecuencia)
        If txt_orden2.Text = "" Then
            txt_idfac.Text = temp2
            cmb_comision.Text = ""
            cmb_serie.Text = ""
            cmb_envio.Text = ""
            cmb_socio.Text = ""
            cmb_forma.Text = ""
            cmb_orden.Text = ""
            cmb_estado.Text = ""
            cmb_total.Text = ""
            dgv_fac.DataSource = ""
        End If

    End Sub

    Private Sub txt_fac2_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_fac2.EnabledChanged
        cmb_serie.Enabled = txt_fac2.Enabled.ToString
        cmb_comision.Enabled = txt_fac2.Enabled.ToString
        cmb_envio.Enabled = txt_fac2.Enabled.ToString
        cmb_socio.Enabled = txt_fac2.Enabled.ToString
        cmb_forma.Enabled = txt_fac2.Enabled.ToString
        cmb_orden.Enabled = txt_fac2.Enabled.ToString
    End Sub

  

   
    Private Sub txt_fac2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_fac2.TextChanged
        If txt_fac2.Text = "" Then
            txt_idfac.Text = ""
        End If
    End Sub

    Private Sub txt_total_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_total.TextChanged

    End Sub
End Class